<?php
  $Level = 1;
  if(!isset($_POST['Typo']) || $_POST['Typo'] == 3) {
	$GroupClause = '';
	}
  if($_POST['Typo'] == 4) {
	$Level = 2;
	$GroupClause = 'OffCode,';
	}
  if (!isset($_POST['Group'])) { 
  	$Group = 'OffCode';} else {$Group = $_POST['Group'];
 	 }
  if (!isset($_POST['Group2'])) { 
  	$Group2 = 1;} else {$Group2 = $_POST['Group2'];
 	 }
  $OLDPAR = 0;
  $TOTAL = '0.00';
  $sub = 1;
  $off = 0;
  $of = 1;
  ?>
<table width="98%"  border="0" cellspacing="0" cellpadding="4">
  <tr height="20">
    <td colspan="3">Designação</td>
    <td colspan="2">Total</td>
  </tr>
  <?php 
	$GET = $GLOBALS['MYD']->db_array("SELECT 
					h.name AS Office,
					h.ref AS OffCode,
					j.name AS User,
					g.name AS CODE,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					g.ParentCODE AS PCOD,
					SUM(a.Amount) AS Exp,
					c.icon,
					k.name AS Provider,
					d.CODE AS ParentName
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN ccenter_contas AS g ON g.CODE IN (a.CODE, b.CODE)
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN ccenter_contas AS d ON d.CODE = g.ParentCODE
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_POST['Time']}' AND '{$_POST['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				AND b.TYPE != 8
				GROUP BY {$GroupClause}g.CODE
				ORDER BY {$GroupClause}g.CODE ASC"); 
				
	$ESG = $GLOBALS['MYD']->db_array("SELECT 
					h.name AS Office,
					h.ref AS OffCode,
					j.name AS User,
					g.name,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					d.name AS PCOD,
					SUM(a.Amount) AS SUB,
					c.icon,
					k.name AS Provider,
					d.CODE AS ParentName
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN ccenter_contas AS g ON g.CODE IN (a.CODE, b.CODE)
				LEFT OUTER JOIN ccenter_contas AS d ON d.CODE = g.ParentCODE
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_POST['Time']}' AND '{$_POST['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				AND b.TYPE != 8
				GROUP BY {$GroupClause}g.ParentCODE
				ORDER BY {$GroupClause}g.ParentCODE"); 
	
	$GSG = $GLOBALS['MYD']->db_array("SELECT 
					h.name AS Office,
					h.ref AS OffCode,
					j.name AS User,
					g.name,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					d.name AS PCOD,
					SUM(a.Amount) AS SUB,
					c.icon,
					k.name AS Provider,
					d.CODE AS ParentName
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN ccenter_contas AS g ON g.CODE IN (a.CODE, b.CODE)
				LEFT OUTER JOIN ccenter_contas AS d ON d.CODE = g.ParentCODE
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_POST['Time']}' AND '{$_POST['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				AND b.TYPE != 8
				GROUP BY OffCode
				ORDER BY OffCode"); 
	
	for ($f = 1; $f <= $GET[0]; $f++) {
	$TOTAL += $GET[$f]['Exp'];
	if ($GroupClause == 'OffCode,' && $off != $GET[$f]['OffCode']) {
		echo '<tr height="16px" bgcolor="#CCCCCC" style="font-weight:bold;">
    <td colspan="3">'.strtoupper($GET[$f]['Office']).'</td>
    <td colspan="3" align="right">'.$GSG[$of]['SUB'].'</td></tr>
	<tr><td colspan="5" height="1px"><hr width="100%" size="1px" /></td></tr>';
		$off = $GET[$f]['OffCode'];
				$of++;
	}
	if ($GET[$f]['PCOD'] != $OLDPAR) { 
	$OLDPAR = $GET[$f]['PCOD'];
		echo '<tr height="16px" style="font-weight:bold;">
    <td colspan="'.($Level).'"></td>
	<td colspan="2"> '.$ESG[$sub]['PCOD'].' '.$ESG[$sub]['ParentName'].'</td>
    <td colspan="2" align="right"> '.$ESG[$sub]['SUB'].'</td></tr>
	<tr><td colspan="5" height="1px"><hr width="100%" size="1px" /></td></tr>';
	$sub++;
	}
	
  if ($GET[$f]['CODTYPE'] == 1) {
     echo '<tr height="20px" bgcolor="'.gridlines($f).'" style="font-weight:bold;">
    <td colspan="'.$Level.'"></td>
	<td>'.$GET[$f]['CODE'].'</td>
    <td colspan="2" align="right">'.$GET[$f]['Exp'].'</td></tr>
	<tr><td colspan="5"><hr width="100%" size="1px" /></td></tr>';
  } elseif ($GET[$f]['CODTYPE'] == 0) {
    echo '<tr height="20px" bgcolor="'.gridlines($f).'">
    <td align="right"><b>'.$GET[$f]['COD'].'</b></td>
    <td width="71%" colspan="2"> '.$GET[$f]['CODE'].'</td>
    <td width="15%" align="right"> '.$GET[$f]['Exp'].'</td>
    <td width="15%" align="right">&nbsp;</td></tr>
	<tr><td colspan="5"><hr width="100%" size="1px" /></td></tr>';
  }
} ?>
   <tr height="20px">
    <td colspan="4">Total:</td>
    <td align="right"><b><?php echo $__sys['CurrencyISO'].' '.$TOTAL;?></b></td>
  </tr>
</table>
